package ec.edu.ucacue.siop.utilidades;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

@SuppressWarnings("deprecation")

public class HibernateUtilSQLServer {
    
    private static final SessionFactory sessionFactory;
	 
    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure("hibernateSQLServer.cfg.xml").buildSessionFactory();
        } 
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
	 
	public static SessionFactory getSessionFactory() {
	return sessionFactory;
	}
	 
	public static Session beginTransaction() {
            Session hibernateSession = HibernateUtilSQLServer.getSession();
            hibernateSession.beginTransaction();
            return hibernateSession;
	}
	 
	public static void commitTransaction() {
	HibernateUtilSQLServer.getSession().getTransaction().commit();
	}
	 
	public static void rollbackTransaction() {
	HibernateUtilSQLServer.getSession().getTransaction().rollback();
	}
	 
	public static void closeSession() {
	HibernateUtilSQLServer.getSession().close();
	}
	 
	public static Session getSession() {
	Session hibernateSession = sessionFactory.getCurrentSession();
	return hibernateSession;
	}
}
